package com.zf.bm.common.core.sys.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zf.bm.annotation.SensitiveData;
import com.zf.bm.annotation.SensitiveField;
import com.zf.bm.common.core.base.entity.BaseEntity;
import com.zf.bm.common.core.config.ConfigUtils;
import com.zf.bm.common.core.mybatis.annotation.FieldIgnore;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户Entity
 */
@TableName("sys_user")

@FieldIgnore({ "updateDate"})
public class User extends BaseEntity<User> {

    private static final long serialVersionUID = 1L;
    private String loginName;// 登录名
    private String password;// 密码
    private String name;    // 姓名
    @SensitiveField
    private String email;    // 邮箱
    @SensitiveField
    private String phone;    // 电话
    private String mobile;    // 手机
    private String loginIp;    // 最后登陆IP
    private Date loginDate;    // 最后登陆日期
    private String loginFlag;    // 是否允许登陆
    private String keyPassword; //key存储密码
    @TableField(fill = FieldFill.INSERT)
    private Integer theme;//主题
    private Integer pwdPeriod;//密码有效期
    @TableField(exist = false)
    private String oldLoginName;// 原登录名
    @TableField(exist = false)
    private String newPassword;    // 新密码
    private Integer modifyPwd;//控制用户登录后修改密码,0登录要修改密码,1不用
    @TableField(exist = false)
    private Role role;    // 根据角色查询用户条件
    @TableField(exist = false)
    private List<Role> roleList = Lists.newArrayList(); // 拥有角色列表
    @TableField(exist = false)
    private List<Long> roleListID = Lists.newArrayList(); // 拥有角色列表
    @TableField(exist = false)
    private String roleName;
    //冻结截止日期
    private Date frozenDate;

    private String usbKey;

//    @IntegrityField
    private String sm3= StringPool.EMPTY;

    @TableField("update_date")
    private Date updateTime;

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getSm3() {
        return sm3;
    }
    public void setSm3(String sm3) {
        this.sm3 = sm3;
    }

    public Date getFrozenDate() {
        return frozenDate;
    }

    public void setFrozenDate(Date frozenDate) {
        this.frozenDate = frozenDate;
    }

    public User() {

        super();
        this.loginFlag = ConfigUtils.YES;
    }

    public User(Long id) {
        super(id);
    }

    public User(Long id, String loginName) {
        super(id);
        this.loginName = loginName;
    }

    public User(Role role) {
        super();
        this.role = role;
    }


    public String getLoginFlag() {
        return loginFlag;
    }

    public void setLoginFlag(String loginFlag) {
        this.loginFlag = loginFlag;
    }

    public Long getId() {
        return id;
    }


    @Length(min = 1, max = 100, message = "登录名长度必须介于 1 和 100 之间")
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @JsonIgnore
    @Length(min = 1, max = 100, message = "密码长度必须介于 1 和 100 之间")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Length(min = 1, max = 100, message = "姓名长度必须介于 1 和 100 之间")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Email(message = "邮箱格式不正确")
    @Length(min = 0, max = 200, message = "邮箱长度必须介于 1 和 200 之间")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Length(min = 0, max = 200, message = "电话长度必须介于 1 和 200 之间")
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Length(min = 0, max = 200, message = "手机长度必须介于 1 和 200 之间")
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getRemarks() {
        return remarks;
    }



    public Date getCreateDate() {
        return createDate;
    }

    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getLoginDate() {
        return loginDate;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    public String getOldLoginName() {
        return oldLoginName;
    }

    public void setOldLoginName(String oldLoginName) {
        this.oldLoginName = oldLoginName;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public Integer getTheme() {
        return theme;
    }

    public void setTheme(Integer theme) {
        this.theme = theme;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Integer getModifyPwd() {
        return modifyPwd;
    }

    public void setModifyPwd(Integer modifyPwd) {
        this.modifyPwd = modifyPwd;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getUsbKey() {
        return usbKey;
    }

    public void setUsbKey(String usbKey) {
        this.usbKey = usbKey;
    }

    public String getKeyPassword() {
        return keyPassword;
    }

    public void setKeyPassword(String keyPassword) {
        this.keyPassword = keyPassword;
    }

    @JsonIgnore
    public List<Role> getRoleList() {
        if (isAdmin()) {
            Role role = new Role(1L);
            role.setEnname("root");
            if (!roleListID.contains(role.getId())) {
                roleList.add(role);
                roleListID.add(role.getId());
            }

        }
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    @JsonIgnore
    public List<Long> getRoleIdList() {
        List<Long> roleIdList = Lists.newArrayList();
        for (Role role : roleList) {
            if(!roleIdList.contains(role.getId())){
                roleIdList.add(role.getId());
            }

        }
        return roleIdList;
    }

    public List<Map> getRoles() {
        List<Map> roleIdList = Lists.newArrayList();
        for (Role role : roleList) {
            Map roleMap = Maps.newHashMap();
            roleMap.put("id", role.getId());
            roleMap.put("name", role.getName());
            roleIdList.add(roleMap);
        }
        return roleIdList;
    }

    public void setRoleIdList(List<Long> roleIdList) {
        roleList = Lists.newArrayList();
        for (Long roleId : roleIdList) {
            Role role = new Role();
            role.setId(roleId);
            roleList.add(role);
        }
    }


    public boolean isAdmin() {
        return isAdmin(this.id);
    }

    public static boolean isAdmin(Long id) {
        return id != null && "1".equals(id.toString());
    }

    public boolean isAdminUser() {
        return isAdminUser(this.id);
    }

    public static boolean isAdminUser(Long id) {
        return id != null && "2".equals(id.toString());
    }

    public Integer getPwdPeriod() {
        return pwdPeriod;
    }

    public void setPwdPeriod(Integer pwdPeriod) {
        this.pwdPeriod = pwdPeriod;
    }

    @Override
    public String toString() {
        return loginName;
    }


    @Override
    protected Serializable pkVal() {
        return id;
    }
}